package day01;

import java.sql.*;

public class TestJDBC1 {

    private static String driverClassName="com.mysql.jdbc.Driver";
    private static String url="jdbc:mysql://127.0.0.1:3306/jdbc?useUnicode=true&characterEncoding=utf8";
    private static String username="root";
    private static String password="123";

    public static void main(String[] args) throws Exception {


        // Driver driver=new com.mysql.jdbc.Driver();
        // DriverManager.registerDriver(driver);

        // Class.forName("com.mysql.jdbc.Driver");

        // Connection con=DriverManager.getConnection(url,username,password);
        //
        // System.out.println("con = " + con);
        //
        // String sql=new StringBuffer()
        //         .append("create table t_user( ")
        //         .append("    id int primary key auto_increment, ")
        //         .append("    name varchar(10) ")
        //         .append(") ")
        //         .toString();
        //
        // Statement stat=con.createStatement();
        //
        // stat.execute(sql);
        //
        // stat.close();
        // con.close();



        // create();

        // insert();

        // update();

        // delete();

        // select();

        // selectById(3);

        // selectByName("bbb");


        selectByName("aaa' or '1'='1");
    }





    public static void create(){
        Connection con=null;
        Statement stat=null;
        try{

            Class.forName(driverClassName);

            con=DriverManager.getConnection(url,username,password);

            String sql=new StringBuffer()
                    .append("create table t_user( ")
                    .append("    id int primary key auto_increment, ")
                    .append("    name varchar(10) ")
                    .append(") ")
                    .toString();


            stat=con.createStatement();

            stat.execute(sql);

        }catch (Exception e){
            e.printStackTrace();
        }finally{
            if(stat!=null){
                try {
                    stat.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if(con!=null){
                try {
                    con.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public static void insert(){
        Connection con=null;
        Statement stat=null;
        try{

            Class.forName(driverClassName);

            con=DriverManager.getConnection(url,username,password);

            String sql=new StringBuffer()
                    .append("insert into t_user ")
                    .append("    (name) ")
                    .append("values ")
                    .append("    ('ccc') ")
                    .toString();

            stat=con.createStatement();

            stat.executeUpdate(sql);


        }catch (Exception e){
            e.printStackTrace();
        }finally{
            if(stat!=null){
                try {
                    stat.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if(con!=null){
                try {
                    con.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public static void update(){
        Connection con=null;
        Statement stat=null;
        try{

            Class.forName(driverClassName);

            con=DriverManager.getConnection(url,username,password);

            String sql=new StringBuffer()
                    .append("update t_user ")
                    .append("set name='abc' ")
                    .append("where id=1 ")
                    .toString();

            stat=con.createStatement();

            stat.executeUpdate(sql);


        }catch (Exception e){
            e.printStackTrace();
        }finally{
            if(stat!=null){
                try {
                    stat.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if(con!=null){
                try {
                    con.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public static void delete(){
        Connection con=null;
        Statement stat=null;
        try{

            Class.forName(driverClassName);

            con=DriverManager.getConnection(url,username,password);

            String sql=new StringBuffer()
                    .append("delete from t_user ")
                    .append("where id=1 ")
                    .toString();

            stat=con.createStatement();

            stat.executeUpdate(sql);


        }catch (Exception e){
            e.printStackTrace();
        }finally{
            if(stat!=null){
                try {
                    stat.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if(con!=null){
                try {
                    con.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public static void select(){
        Connection con=null;
        Statement stat=null;
        ResultSet rs=null;
        try{

            Class.forName(driverClassName);

            con=DriverManager.getConnection(url,username,password);

            String sql=new StringBuffer()
                    .append("select * ")
                    .append("from t_user ")
                    .toString();

            stat=con.createStatement();

            rs=stat.executeQuery(sql);

            while(rs.next()){
                // int id=rs.getInt(1);
                // String name=rs.getString(2);

                int id=rs.getInt("id");
                String name=rs.getString("name");

                System.out.println(id+"\t"+name);
            }


        }catch (Exception e){
            e.printStackTrace();
        }finally{
            if(rs!=null){
                try {
                    rs.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if(stat!=null){
                try {
                    stat.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if(con!=null){
                try {
                    con.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public static void selectById(int id){
        Connection con=null;
        Statement stat=null;
        ResultSet rs=null;
        try{

            Class.forName(driverClassName);

            con=DriverManager.getConnection(url,username,password);

            String sql=new StringBuffer()
                    .append("select * ")
                    .append("from t_user ")
                    .append("where id="+id)
                    .toString();

            stat=con.createStatement();

            rs=stat.executeQuery(sql);

            if(rs.next()){
                // int id=rs.getInt(1);
                // String name=rs.getString(2);

                id=rs.getInt("id");
                String name=rs.getString("name");

                System.out.println(id+"\t"+name);
            }


        }catch (Exception e){
            e.printStackTrace();
        }finally{
            if(rs!=null){
                try {
                    rs.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if(stat!=null){
                try {
                    stat.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if(con!=null){
                try {
                    con.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
    }


    public static void selectByName(String name){
        Connection con=null;
        Statement stat=null;
        ResultSet rs=null;
        try{

            Class.forName(driverClassName);

            con=DriverManager.getConnection(url,username,password);

            String sql=new StringBuffer()
                    .append("select * ")
                    .append("from t_user ")
                    .append("where name='"+name+"'")
                    .toString();

            stat=con.createStatement();

            rs=stat.executeQuery(sql);

            while(rs.next()){
                // int id=rs.getInt(1);
                // String name=rs.getString(2);

                int id=rs.getInt("id");
                name=rs.getString("name");

                System.out.println(id+"\t"+name);
            }


        }catch (Exception e){
            e.printStackTrace();
        }finally{
            if(rs!=null){
                try {
                    rs.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if(stat!=null){
                try {
                    stat.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if(con!=null){
                try {
                    con.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
    }


}
